/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.searchWindow
{
	import com.edi.proxy.component.EPOParam;
	import com.edi.proxy.component.HttpProxy;
	import com.edi.proxy.component.HttpProxyResult;
	import com.edi.runtime.component.EDIBasicData;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.collections.ListCollectionView;
	import mx.collections.XMLListCollection;
	import mx.containers.Canvas;
	import mx.controls.ComboBox;
	import mx.controls.DataGrid;
	import mx.controls.DateField;
	import mx.controls.NumericStepper;
	import mx.controls.TextInput;
	import mx.events.ListEvent;
	import mx.events.NumericStepperEvent;

	public class DataEditor extends Canvas
	{
        private var newValue:DisplayObject = null;
        private var txt:TextInput;
        private var df:DateField;
        private var cb:ComboBox;
        private var ns:NumericStepper;
        private var bc:TextInput;
        private var bd:EDIBasicData;
        private var dataChanged:Boolean=false;
        
        
		public function DataEditor()
		{
			super();
			horizontalScrollPolicy="off";
			verticalScrollPolicy="off";
		}
		
		override public function set data(value:Object):void
        {
            super.data = value;
            
            dataChanged = true;
            invalidateProperties();
        }
        
        override protected function commitProperties():void
        {
            super.commitProperties();
            
            if (dataChanged)
            {
                createEditor();
                dataChanged = false;
            }
        }
        
        private function createEditor():void
        {
            removeEditor();
            if(data.type=="textinput")
            {
            	if(txt==null){
            		txt=new TextInput();
            		txt.addEventListener(Event.CHANGE,function(event:Event):void
            			{
            				data.value=txt.text;
            			}
            		);
            	}
            	newValue=txt;
            }
            else if(data.type=="datefield")
            {
            	if(df==null)
            	{
            		df=new DateField();
            		df.formatString="YYYY-MM-DD";
            		df.addEventListener(Event.CHANGE,function(event:Event):void
            			{
            				data.value=df.text;
            			}
            		);
            	}
            	newValue=df;
            }
        	else if(data.type=="numericstepper")
        	{
        		if(ns==null)
            	{
            		ns=new NumericStepper();
            		ns.maximum=1000;
            		ns.addEventListener(NumericStepperEvent.CHANGE,function(event:NumericStepperEvent):void
	            		{
	            			data.value=ns.value;
	            		}
            		);
            	}
            	newValue=ns;
        	}
            else if(data.type=="combobox")
            {
            	if(cb==null)
            	{
            		cb=new ComboBox();
            		cb.dataProvider=data.dataProvider;
            		cb.labelField=data.labelField;
            		cb.addEventListener(ListEvent.CHANGE,function(event:ListEvent):void
            			{
            				data.value=cb.selectedItem[data.dataField];
            			}
            		);
            	}
            	newValue=cb;
            }
            else if(data.type=="baseComponent")
            {
            	if(bc==null)
            	{
            		bd=new EDIBasicData();
            		bd.displayField="caption";
            		bd.callback=handlerCallBack;
					bd.width=0;
					bd.height=0;
					data.dataBaseBtn=bd;
					addChild(bd);
            		bc=new TextInput();
            		bc.editable=false;
            	}
            	newValue=bc;
            }
            else if(data.type=="SQL")
            {
            	if(cb==null)
            	{
            		var epo:EPOParam=new EPOParam();
            		cb=new ComboBox();
            		cb.height=20;
            		cb.labelField=data.labelField;
            		cb.addEventListener(ListEvent.CHANGE,function(event:ListEvent):void
            			{
            				data.value=cb.selectedItem[data.dataField];
            			}
            		);
            		
            		epo.EQL="select "+data.labelField+","+data.dataField+" from "+data.tableName;
            		epo.dsName="get"+data.tableName;
            		var proxy:HttpProxy=new HttpProxy();
            		proxy.addPoParam(epo);
            		proxy.resultCallback=function(event:HttpProxyResult):void
            		{
            			var xml:XML=XML("<DB><row><"+data.dataField+" fieldType=\"string\" />"+
            				"<"+data.labelField+" fieldType=\"string\" /></row></DB>");
            			xml.appendChild(event.getPoResultByDsName("get"+data.tableName).table.children());;
            			cb.dataProvider=xml.children();
            		}
            		proxy.send();
            	}
            	newValue=cb;
            }
            
            
            if(bd!=null)
            {
            	bd.saveField = "";
				bd.treeIDField = "";
				bd.treepidField = "";
				bd.displayField = "";
				bd.treeLabelField = "";
				bd.remoteService = "";
				bd.treeItemID = "";
				bd.treeRootValue = "";
				bd.itemParentID = "";
				bd.itemID="";
				
				bd.setFEDIBasicDataWindowISNULL();
				
        	    bd.saveField = data.saveField;
				bd.treeIDField = data.treeIDField;
				bd.treepidField = data.treepidField;
				bd.displayField = data.displayField;
				bd.treeLabelField = data.treeLabelField;
				bd.remoteService = data.remoteService;
				bd.treeItemID = data.treeItemID;
				bd.treeRootValue = data.treeRootValue;
				bd.itemParentID = data.itemParentID;
				bd.itemID=data.itemID;
            }
            if(newValue!=null)
            {
	            newValue.x=0;
	            newValue.y=0;
	            newValue.width=100;
	            newValue.height=24;
	            addChild(newValue);
            }
        }
        
        private function removeEditor():void
        {
           if(newValue!=null)
           {
           		removeChild(newValue);
           		newValue=null;
           } 
        }
        
		private function handlerCallBack():void
		{
			if(bd.returnArr.length>0)
			{
				data.value=bd.returnArr[0]["caption"];
				bc.text=data.value;
				data.handlerID=bd.returnArr[0]["id"];
				((this.parent.parent as DataGrid).dataProvider as ListCollectionView).refresh();
			}
		}
	}
}